2 // FlatBezelObjectCell.swift
5 // Created by Ruben Beltran del Rio on 2/8/23.
6 // Copyright © 2023 BRNBW. All rights reserved.
12 class FlatBezelObjectCell: QSObjectCell {
13 let preferredImagePosition: NSControl.ImagePosition = .imageAbove
15 override func draw(withFrame cellFrame: NSRect, in controlView: NSView!) {
16 let isFirstResponder = controlView.window?.firstResponder == controlView && !controlView.isKind(of: NSTableView.self)
18 let dropTarget = self.isHighlighted && self.highlightsBy.contains(NSCell.StyleMask.changeBackgroundCellMask) && !self.isBezeled
20 var fillColor: NSColor = self.backgroundColor ?? .textBackgroundColor
21 if (isFirstResponder) {
22 fillColor = self.highlightColor()
25 fillColor = NSColor(red: 0.77, green: 0.91, blue: 0.96, alpha: 1)
28 var strokeColor: NSColor = .clear
31 strokeColor.setStroke()
33 let roundRect = NSBezierPath()
34 roundRect.appendRoundedRect(cellFrame, xRadius: cellRadiusFactor(), yRadius: cellRadiusFactor())
37 self.drawInterior(withFrame: self.drawingRect(forBounds: cellFrame), in: controlView)
40 override func titleRect(forBounds rect: NSRect) -> NSRect {
41 super.titleRect(forBounds: rect.offsetBy(dx: 0, dy: -4))
44 override func drawText(for drawObject: QSObject!, withFrame cellFrame: NSRect, in controlView: NSView!) {
45 if self.imagePosition == .imageOnly {
49 var abbrString: String? = nil
50 if controlView.responds(to: #selector(QSSearchObjectView.matchedString)) {
51 abbrString = (controlView as! QSSearchObjectView).matchedString()
54 var nameString: String? = drawObject.displayName()
55 var hitMask: AutoreleasingUnsafeMutablePointer<NSIndexSet?>? = nil
57 var ranker = drawObject.ranker()
58 if let ranker, let abbrString {
59 nameString = ranker.matchedString(forAbbreviation: abbrString, hitmask: hitMask, inContext: nil)
62 var rankedStringIsName = nameString == drawObject.displayName()
63 if nameString == nil {
64 nameString = drawObject.identifier() ?? "Unknown"
67 var useAlternateColor = false
68 if let controlView = controlView as? NSTableView {
69 useAlternateColor = controlView.isRowSelected(controlView.row(at: cellFrame.origin))
72 var mainColor: NSColor? = textColor()
74 mainColor = useAlternateColor ? .alternateSelectedControlTextColor : .controlTextColor
77 var fadedColor = mainColor!.withAlphaComponent(0.50)
78 var textDrawRect = titleRect(forBounds: cellFrame)
80 var titleString = NSMutableAttributedString(string: nameString!)
81 titleString.setAttributes(rankedStringIsName ? nameAttributes : detailAttributes, range: NSMakeRange(0, titleString.length))
83 if abbrString != nil && abbrString!.hasPrefix("QSActionMnemonic") {
84 titleString.addAttribute(.foregroundColor, value: rankedStringIsName ? fadedColor : fadedColor.withAlphaComponent(0.8), range: NSMakeRange(0, titleString.length))
97 NSUInteger hits[[titleString length]];
98 NSUInteger count = [hitMask getIndexes:(NSUInteger *)&hits maxCount:[titleString length] inIndexRange:nil];
99 NSDictionary *attributes = @{
100 NSForegroundColorAttributeName: rankedStringIsName ? mainColor : fadedColor
102 for(i = 0; i<count; i += j) {
103 for (j = 1; i+j<count && hits[i+j-1] +1 == hits[i+j]; j++);
104 [titleString addAttributes:attributes range:NSMakeRange(hits[i], j)];
107 [titleString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, [titleString length])];
111 NSString *detailsString = [drawObject details];
113 NSRange returnRange = [detailsString rangeOfString:@"\n"];
114 if (returnRange.location != NSNotFound) {
115 detailsString = [detailsString substringToIndex:returnRange.location];
118 detailsAttributes = [detailsAttributes mutableCopy];
119 [detailsAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
121 if (detailsString && detailsString.length && ![detailsString isEqualToString:nameString]) {
122 [titleString appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",detailsString] attributes:detailsAttributes] autorelease]];
126 NSRect centerRect = rectFromSize([titleString size]);
127 centerRect.size.width = NSWidth(textDrawRect);
128 centerRect.size.height = MIN(NSHeight(textDrawRect), centerRect.size.height);
129 [titleString drawInRect:centerRectInRect(centerRect, textDrawRect)];